package com.lizhi.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.alibaba.fastjson.JSON;

public class Util {
	static ExecutorService executorService=Executors.newFixedThreadPool(1);
	public static void execute(Runnable runnable){
		executorService.execute(runnable);
	}
	public static String getBookURL(){
		return "https://fbook.firebaseio.com/";
	}
	public synchronized static Document getDocument(String url) throws Exception {
		return Jsoup.connect(url).get();
//		HttpURLConnection connection = null;
//		try {
//			
//			 connection=(HttpURLConnection) new URL(url).openConnection();
//			connection.setRequestMethod("GET");
//			connection.setConnectTimeout(1000);
//			connection.connect();
//			
////			httpClient = new DefaultHttpClient();
////			HttpGet httpGet = new HttpGet(url);
////			CloseableHttpResponse response = httpClient.execute(httpGet);
//			InputStream inputStream = connection.getInputStream();
//			String string = Util.readStream(inputStream);
//			Document document = Jsoup.parse(string);
//			connection.disconnect();
////			httpGet.abort();
////			httpGet.completed();
////			httpClient.close();
//			return document;
//		} catch (Exception e) {
//			e.printStackTrace();
//		} finally {
//			connection.disconnect();
//		}
//		return null;
	}

	public static String readStream(InputStream inputStream) {
		StringBuffer strContent = new StringBuffer();
		String strLine;
		try {
			InputStreamReader streamReader = new InputStreamReader(inputStream,
					"GBK");
			BufferedReader reader = new BufferedReader(streamReader);
			while ((strLine = reader.readLine()) != null) {
				strContent.append(strLine);
				strContent.append("\n");
			}
			reader.close();
			streamReader.close();
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return strContent.toString();
	}

	public static Map<String, String> readBookBusiness(String fileName) {
		java.util.Map<String, String> map = new HashMap<String, String>();
		try {
			FileInputStream inputStream = new FileInputStream(
					new File(fileName));
			String strLine;
			InputStreamReader streamReader = new InputStreamReader(inputStream);
			BufferedReader reader = new BufferedReader(streamReader);
			while ((strLine = reader.readLine()) != null) {
				String[] temps = strLine.split("&&&");
				map.put(temps[0], temps[1]);
			}
			streamReader.close();
			reader.close();
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return map;
	}

	public static String readLyric(String path) {
		File file = new File(path);
		FileInputStream inputStream = null;
		try {
			inputStream = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return readStream(inputStream);
	}

	public static void clearData(String url) {

		String rootURL = url;
		CloseableHttpClient httpClient = null;
		try {
			System.out.println(rootURL);
			 httpClient = HttpClients.createDefault();
			HttpPut httpPost = new HttpPut(rootURL);
			httpPost.setEntity(new StringEntity(JSON.toJSONString(null),
					"utf-8"));
			CloseableHttpResponse response = httpClient.execute(httpPost);
			InputStream inputStream = response.getEntity().getContent();
			System.out.println(Util.readStream(inputStream));
			response.close();
			httpPost.abort();
			httpPost.completed();
//			httpClient.close();
		} catch (Exception e) {
			System.err.println(rootURL);
			e.printStackTrace();
		}finally{
			if(httpClient!=null){
				try {
					httpClient.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	public static void saveFile(String name, String content) {
		try {
			File file = new File(name);
			FileWriter writer = new FileWriter(file);
			writer.write(content);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void upload(String url, String json) throws Exception {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		httpPost.setEntity(new StringEntity(json, "utf-8"));
		CloseableHttpResponse response = httpClient.execute(httpPost);

		InputStream inputStream = response.getEntity().getContent();

		System.out.println(Util.readStream(inputStream));
		response.close();
		httpPost.abort();
		httpPost.completed();
		httpClient.close();
	}

	public static void uploadPut(String url, String json) throws Exception {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPut httpPut = new HttpPut(url);
		httpPut.setEntity(new StringEntity(json, "utf-8"));
		CloseableHttpResponse response = httpClient.execute(httpPut);

		InputStream inputStream = response.getEntity().getContent();

		System.out.println(Util.readStream(inputStream));
		response.close();
		httpPut.abort();
		httpPut.completed();
		httpClient.close();
	}
}
